package com.leoman.plz_p.presenter;

import com.leoman.plz_p.interfaces.HomePageView;
import com.leoman.plz_p.module.api.MainApi;
import com.leoman.plz_p.module.bean.BannerBean;
import com.leoman.plz_p.module.bean.BaseData;
import com.leoman.plz_p.module.bean.CategoryBean;
import com.leoman.plz_p.module.bean.CourseBean;
import com.leoman.plz_p.module.bean.MapData;
import com.leoman.plz_p.module.bean.PageListData;
import com.leoman.plz_p.subscriber.ResponseSubscriber;

import cn.com.corelib.api.ResponseTransformer;
import cn.com.corelib.base.BasePresenter;

public class HomePagePresenter extends BasePresenter<HomePageView> {

    private MainApi mainApi;

    @Override
    public void onStart() {
        getBannerList();
        getCategory();
        getHotCourse(null);
        getFineCourse(null);
    }

    @Override
    protected void onViewAttach() {
        super.onViewAttach();
        mainApi=getApi(MainApi.class);
    }

    public void getBannerList(){
        mainApi.getBanner().compose(new ResponseTransformer<>(this.<BaseData<MapData<BannerBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<MapData<BannerBean>>>(view) {
                    @Override
                    public void success(BaseData<MapData<BannerBean>> listData) {
                        view.obtainBanner(listData.data.list);
                    }

                });
    }

    public void getCourseList(String name, final boolean search){
        mainApi.getCourseList(name,1,8).compose(new ResponseTransformer<>(this.<BaseData<PageListData<CourseBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<CourseBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<CourseBean>> listData) {
                        if (checkListNotNull(listData.data.getRows())){
                            view.obtainCourseList(listData.data.getRows(),search);
                        }else {
                            if (search){
                                view.obtainCourseList(listData.data.getRows(),search);
                            }
                        }
                    }

                });
    }


    public void getHotCourse(String name){
        mainApi.getHotCourse(1,4,name).compose(new ResponseTransformer<>(this.<BaseData<PageListData<CourseBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<CourseBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<CourseBean>> listData) {
                        if (checkListNotNull(listData.data.getRows())){
                            view.obtainHotCourseList(listData.data.getRows());
                        }
                    }

                });
    }

    public void getFineCourse(String name){
        mainApi.getFineCourse(1,4,name).compose(new ResponseTransformer<>(this.<BaseData<PageListData<CourseBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<CourseBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<CourseBean>> listData) {
                        if (checkListNotNull(listData.data.getRows())){
                            view.obtainFineCourseList(listData.data.getRows());
                        }
                    }

                });
    }

    public void getCategory(){
        mainApi.getCategory(1,8).compose(new ResponseTransformer<>(this.<BaseData<PageListData<CategoryBean>>>bindToLifeCycle()))
                .subscribe(new ResponseSubscriber<BaseData<PageListData<CategoryBean>>>(view) {
                    @Override
                    public void success(BaseData<PageListData<CategoryBean>> pageListDataBaseData) {
                        view.obtainCategory(pageListDataBaseData.data.getRows());
                    }
                });
    }
}